import pymysql


class Mysql:

    def __init__(self):
        self.conn = pymysql.connect(host="localhost",user="root",password="root",database="school",charset="utf8")

    # 主要做增删改操作
    def execute(self,sql):
        try:
            with self.conn.cursor() as cursor:
                print("执行的sql语句:{}".format(sql))
                cursor.execute(sql)
        except Exception as e:
            print("执行SQL错误:{}".format(e))

    # 主要做查询
    def get_data(self,sql):
        try:
            with self.conn.cursor() as cursor:
                print("执行的sql语句:{}".format(sql))
                cursor.execute(sql)
                result = cursor.fetchall()
                return result
        except Exception as e:
            print("执行SQL错误:{}".format(e))

    def close(self):
        self.conn.close()


